package cn.gorit.admin.controller;

import cn.gorit.admin.service.ContentService;
import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.content.BlogDto;
import cn.gorit.entity.content.About;
import cn.gorit.entity.content.Blog;
import cn.gorit.utils.Page;
import cn.gorit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @Classname ContentController
 * @Description 内容管理
 * @Author CodingGorit
 * @Created on 2021/4/16 0:19
 * @IDLE IntelliJ IDEA
 **/
@RestController
@RequestMapping("/api/v1/content")
public class ContentController {


    @Autowired
    private ContentService contentService;
    // ==========================  帖子内容列表，以及帖子查看的后台接口
    // 查询帖子列表 /api/v1/content/back/blog/page
    @PostMapping("/back/blog/page")
    public Result<Page<Blog>> getBlogDtoPage(@RequestBody Page<Blog> page) {
        return contentService.getBlogListByPage(page);
    }

    // 查询帖子具体信息 /api/v1/content/back/blog/detail/xxx
    @GetMapping("/back/blog/detail/{blogId}")
    public Result<Blog> getBlogById(@PathVariable("blogId") String blogId) {
        return contentService.getBlogById(blogId);
    }

    // 修改帖子状态 /api/v1/content/back/blog/{blogId}/{deleted}
    @PutMapping("/back/blog/top/{status}/{blogId}")
    public Result<Object> updateTop(@PathVariable("status") Integer status, @PathVariable("blogId") String blogId) {
        return contentService.updateBlogTopById(status, blogId);
    }
    // ==========================  关于我的相关
    /**
     * 保存关于信息  OK
     * @param about
     * @return
     */
    @RequestMapping(value = "/back/about",method = RequestMethod.POST)
    public Result<Object> saveAbout(@RequestBody About about) {
        contentService.saveAbout(about);
        return new Result<>("添加成功");
    }

    /**
     * 修改 OK 需要优化一下，才行，添加参数注意 version 字段，乐观锁保证数据能正常使用
     */
    @PutMapping("/back/about")
    public Result<Object> updateAbout(@RequestBody About about) {
        contentService.updateAbout(about);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
    }

    /**
     * 根据 ID 查询 （后台） OK
     */
    @RequestMapping(value = "/back/about/{id}",method = RequestMethod.GET)
    public Result<About> getAboutById(@PathVariable("id") Integer id) {
        About about = contentService.getAboutById(id);
        return new Result<About>(about != null ?"查询成功":"数据不存在", about);
    }

    /**
     * 根据 ID 删除 （逻辑删除） OK
     */
    @RequestMapping(value = "/back/about/{id}",method = RequestMethod.DELETE)
    public Result<Object> deleteAboutById(@PathVariable("id") Integer id) {
        contentService.deleteAboutById(id);
        return new Result<>(ResultEnums.DELETE_SUCCESS.getCode(),ResultEnums.DELETE_SUCCESS.getMsg());
    }

    /**
     * 启用 与 禁用
     */
    @RequestMapping(value = "/back/about/{id}/{enable}",method = RequestMethod.PUT)
    public Result<Object> enableAbout(@PathVariable("id") Integer id,@PathVariable("enable") Integer enable) {
        return contentService.enableAboutById(id,enable);
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/back/about/page",method = RequestMethod.POST)
    public Result<Page<About>> getByPage(@RequestBody Page<About> page) {
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"about_id", "created_time", "update_time"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page = contentService.getAboutByPage(page);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    // TODO
}
